﻿using System;
using System.Collections.Generic;
using System.Text;
using BOHVote.DAL;
using System.Data;
using BOHVote.Entity;

namespace BOHVote.BLL
{
    public static class UserClass
    {

        #region <<  User  >>
        public static DataSet getListUserDataSet(string UserType,string GovernID)
        {
            UserProvide up = new UserProvide();
            DataSet dstUser = up.getUserList(UserType, GovernID);

            return dstUser;
        }
        public static DataSet getListUserDataSet()
        {
            UserProvide up = new UserProvide();
            DataSet dstUser = up.getUserList("","");

            return dstUser;
        }

        public static BHUser getUser(string strUserID, string password, string userType)
        {
            UserProvide up = new UserProvide();
            DataSet dstUser = up.getUser(strUserID, password, userType);
            BHUser bU = null;
            if (dstUser.Tables[0].Rows.Count > 0)
            {
                bU = new BHUser();
                bU.GovernID = dstUser.Tables[0].Rows[0]["GovernID"].ToString();
                bU.UserID = dstUser.Tables[0].Rows[0]["ID"].ToString();
                bU.Mail = dstUser.Tables[0].Rows[0]["mail"].ToString();
                bU.password = dstUser.Tables[0].Rows[0]["password"].ToString();
                bU.UserName = dstUser.Tables[0].Rows[0]["user_name"].ToString();
                bU.Phone = dstUser.Tables[0].Rows[0]["phone"].ToString();
                bU.UserType = dstUser.Tables[0].Rows[0]["user_type"].ToString();
                bU.UnitID = dstUser.Tables[0].Rows[0]["UnitID"].ToString();
                bU.UserAccess = dstUser.Tables[0].Rows[0]["RightType"].ToString();
                bU.UserCode = dstUser.Tables[0].Rows[0]["User_ID"].ToString();
                bU.RightCode = dstUser.Tables[0].Rows[0]["rightCode"].ToString();
                bU.RightType = dstUser.Tables[0].Rows[0]["RightType"].ToString();
                if (bU.RightType == "G" || bU.RightType == "U")
                {
                    string ApplyConfirmed = string.Empty;
                    string AllowApply = string.Empty;
                    DataSet dstGovern = UnitClass.getGovernByGovernID(bU.GovernID);
                    if (dstGovern != null && dstGovern.Tables[0].Rows.Count > 0)
                    {
                        ApplyConfirmed = dstGovern.Tables[0].Rows[0]["ApplyConfirmedInd"].ToString();
                       //AllowApply = dstGovern.Tables[0].Rows[0]["AllowApply"].ToString();
                    }
                    if (ApplyConfirmed == "N")
                        bU.CanApplyGrovenUnit = false;
                    else
                        bU.CanApplyGrovenUnit = true;
                }
            }
            return bU;
        }

        public static DataSet getUserListBySearch(string username, string usertype)
        {
            UserProvide up = new UserProvide();
            DataSet dstUser = up.getUserListBySearch(username, usertype);

            return dstUser;
        }

        public static int UpdateUser(BHUser user)
        {
            UserProvide up = new UserProvide();
            return up.UpdateUser(user.UserCode,user.UserID,user.UserName,user.password,user.UnitID,user.GovernID,user.UserType,user.Status,user.Phone,user.Mail);
        }

        public static string InsertUser(BHUser user)
        {
            UserProvide up = new UserProvide();

            return up.InsertUser(user.UserCode, user.UserName, user.password, user.UnitID, user.GovernID, user.UserType, user.Status, user.Phone, user.Mail);
        }

        public static int DeleteUser(string UserID)
        {
            UserProvide up = new UserProvide();
            return up.DeleteUser(UserID);
        }

        public static BHUser getUserByID(string userID)
        {
            UserProvide up = new UserProvide();
            DataSet dst = up.SingleUser(userID);
            BHUser bu = new BHUser();

            bu.GovernID = dst.Tables[0].Rows[0]["GovernID"].ToString();
            bu.Mail = dst.Tables[0].Rows[0]["mail"].ToString();
            bu.password = dst.Tables[0].Rows[0]["password"].ToString();
            bu.Phone = dst.Tables[0].Rows[0]["phone"].ToString();
            bu.Status = dst.Tables[0].Rows[0]["status"].ToString();
            bu.UnitID = dst.Tables[0].Rows[0]["UnitID"].ToString();
            bu.UserName = dst.Tables[0].Rows[0]["user_name"].ToString();
            bu.UserType = dst.Tables[0].Rows[0]["user_type"].ToString();
            bu.UserCode = dst.Tables[0].Rows[0]["user_id"].ToString();

            return bu;
        }

        public static List<BHUser> getListUser()
        {
            UserProvide up = new UserProvide();
            List<BHUser> BHUserList = new List<BHUser>();
            DataSet dstUser = up.getUserList("","");
            foreach(DataRow dr in dstUser.Tables[0].Rows)
            {
                BHUser bu = new BHUser();
                bu.GovernID = dr["GovernID"].ToString();
                bu.Mail = dr["mail"].ToString();
                bu.password = dr["mail"].ToString();
                bu.Phone = dr["phone"].ToString();
                bu.Status = dr["status"].ToString();
                bu.UnitID = dr["UnitID"].ToString();
                bu.UserName = dr["user_name"].ToString();
                bu.UserType = dr["user_type"].ToString();
                bu.RightCode = dr["RightCode"].ToString();
                bu.RightType = dr["RightType"].ToString();
                BHUserList.Add(bu);
            }

            return BHUserList;
        }

        public static int ModifyPassword(string ID, string password)
        {
            UserProvide up = new UserProvide();
            return up.ModifyPassword(ID, password);
        }

        /// <summary>
        /// 判断用户是否已经存在
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public static bool HasUserCode(string UserCode)
        { 
            bool ReturnValue = false;
             UserProvide up = new UserProvide();
             DataSet dst = up.getUserByUserCode(UserCode);
            if(dst.Tables.Count>0&&dst.Tables[0].Rows.Count>0)
            {
                ReturnValue = true;
            }

            return ReturnValue;
        }
        #endregion

        #region << User Right >>
        public static DataSet getAllAccess(string UserName, string RightType)
        {
            UserProvide up = new UserProvide();
            return up.dstUserRightList(UserName, RightType, true);
        }

        public static int deleteAccess(string AccessID)
        {
            UserProvide up = new UserProvide();
            return up.DeleteAccessByID(AccessID);
        }

        public static string getUserAllAccess(string UserID)
        {
            UserProvide up = new UserProvide();
            return up.getUserAccessByUserID(UserID,true);
        }

        public static DataSet dstUserAccessByAccessID(string AccessID)
        {
            UserProvide up = new UserProvide();
            return up.dstUserRightByAccessID(AccessID);
        }

        public static int UpdateUserAccess(string AccessID,string RightID,string UserID)
        {
            UserProvide up = new UserProvide();
            return up.UpdateUserAccess(AccessID, RightID,UserID);
        }

        public static int InsertUserAccess(string UserID, string RightID,string Status)
        {
            UserProvide up = new UserProvide();
            return up.InsertUserAccess(UserID, RightID, Status);
        }

        #endregion

        #region << access right  >>
        public static DataSet dstGetRight()
        { 
            UserProvide up = new UserProvide();
            return up.dstGetAllRight();
        }

        public static int updateRight(string rightID,string rightName,string rightType,string rightCode)
        {
            UserProvide up = new UserProvide();
            return up.UpdateRight(rightID, rightName, rightType,rightCode);
        }

        public static int InsertRight(string rightName, string rightCode, string rightType)
        { 
            UserProvide up = new UserProvide();
            return up.InsertRight(rightName, rightCode, rightType);
        }
        #endregion
    }
}
